@model MvcCms.Store.Web.ViewModels.DefaultViewModel
@helper Truncate(string input, int length)
    {    
    if (input != null)
    {
        if (input.Length <= length)
        {
            @input
        }
        else
        {
            @input.Substring(0, length)<text>...</text>
        }
    }
}
<div class="contentpage-outerdiv">
    <h2>Mange Store Images</h2>
    @using (Html.BeginForm("ImageManager", "StoreManager", FormMethod.Post, new { id = "form1", enctype = "multipart/form-data" }))
    {
        <div>
            <div>Upload Images</div>
            <div>
                <table>
                    <tr>
                        <td>
                            Single File:
                        </td>
                        <td>
                            <input type="file" id="File2" name="singlefile" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" id="Submit1" value="Upload" />
                        </td>                    
                    </tr>
                </table>
            </div>
        </div>
        <div>
            <fieldset>
                <legend>@Html.Encode(Model._Files.Count().ToString()) Images Detected</legend>                                      
                <table border="1">
        @{
        int i = 0;
        foreach (var file in Model._Files)
        {
            if ((i % 4) == 0)
            {                                
                @Html.Raw("<tr><td>")             
            }
            else
            {                                
                @Html.Raw("<td>")             
            }                        
                    @Html.Raw("<table id='cell-" + i + "'>") 
                    <tr>
                    <td>
                        <a href="/Uploadedimages/@Model._Portal.URLAuthority/@file.Name">
                            <img border="0" alt="@Html.Encode(file.Name)" src="/Uploadedimages/@Model._Portal.URLAuthority/thumbs/@file.Name" />
                        </a>
                    </td>
                    <td>
                        @Truncate(file.Name, 10) <br />
                        @Html.Encode(file.LastWriteTime.ToString("d"))                        
                    </td>
                    <td>
                        <a onclick="DeleteImage('@file.Name ', @i )" href="javascript:void(0)">Del</a>     
                    </td>    
                    </tr>
                    @Html.Raw("</table>") 
                          
            if ((i % 2) != 0)
            {                                                  
                @Html.Raw("</td></tr>")                                                                                                     
            }
            else
            {
                @Html.Raw("</td>")
            }
            i += 1;
        }
    }
                </table>
            </fieldset>
        </div>
    } 
</div>

<script type="text/javascript">
    function DeleteImage(imageToDelete, cellnum) {
        var data = imageToDelete;
        $.post("/storemanager/deleteimage",
        {
            ImageName:imageToDelete,
            PortalID:@Model._Portal.PortalID
        },
        function (data) {
            var objdata = data;
            if (objdata.error) {
                alert(objdata.error);
            }
            else {
                if (objdata.result == "success") {
                    RemoveFileContainer(cellnum);
                }
                else {
                    alert("There was a problem removing the image.");
                }
            }
        },"json");
    }
    function RemoveFileContainer(rownum) {
        $("#cell-" + rownum).fadeOut("normal", function () {
            $(this).remove();
        });
    }
</script>

